package com.hqyj.javaSpringBoot.modules.test.controller;

import com.hqyj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.javaSpringBoot.modules.common.entity.SearchBean;
import com.hqyj.javaSpringBoot.modules.test.entity.Student;
import com.hqyj.javaSpringBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description：StudentController
 * @Author ：Cwd
 * @Date ：2021/8/20 14:52
 */
@RestController
@RequestMapping("/api")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 127.0.0.1:8083/api/student ---- post
     * {"name":"Cwd","email":"190982383@qq.com","age":"21"}
     * {"name":"Cwd","email":"cwd_1999@163.com","age":"21","studentCard":{"cardNo":"201806084135"}}
     * @param student
     * @return
     */
    @PostMapping(value = "/student", consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }

    //insert 与 update 操作所用方法一致，
    // 传入的JSON数据若无唯一标识符，则为insert操作；若有唯一标识符，则为update操作

    /**
     * 127.0.0.1:8083/api/student_edit ---- post
     * {"name":"Cwd","email":"190982383@qq.com","age":"21"}
     * {"name":"Cwd","email":"cwd_1999@163.com","age":"21","studentCard":{"cardNo":"201806084135"}}
     * {"id":"1","name":"dpy","email":"dpy@123.com","age":"21","studentCard":{"cardNo":"201806084107"}}
     * @param student
     * @return
     */
    @PostMapping(value = "/student_edit", consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.editStudent(student);
    }

    /**
     * 127.0.0.1:8083/api/student/8 ---- delete
     * @param id
     * @return
     */
    @DeleteMapping("/student/{id}")
    public Result<Object> deleteStudentById(@PathVariable    int id) {
        return studentService.deleteStudentById(id);
    }

    /**
     * 127.0.0.1:8083/api/student/5 ---- get
     * @param id
     * @return
     */
    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable int id) {
        return studentService.getStudentById(id);
    }

    /**
     * 127.0.0.1:8083/api/students ---- get
     * @return
     */
    @GetMapping("/students")
    public List<Student> getStudents() {
        return studentService.getStudents();
    }

    /**
     * 127.0.0.1:8083/api/student?name=Cwd&email=cwd ---- get
     * @param name
     * @param email
     * @return
     */
    @GetMapping("/student")
    public Student getStudentByNameAndEmail(@RequestParam String name,
                                           @RequestParam String email) {
        return studentService.getStudentByNameAndEmail(name,email);
    }

    /**
     * 127.0.0.1:8083/api/students ---- post
     * {"currentPage":1,"pageSize":5,"orderBy":"name","direction":"desc","keyword":""}
     * @param searchBean
     * @return
     */
    @PostMapping(value = "/students", consumes = "application/json")
    public Page<Student> getStudentsBySearchBean(@RequestBody SearchBean searchBean) {
        return studentService.getStudentsBySearchBean(searchBean);
    }

    /**
     * 127.0.0.1:8083/api/students/v2 ---- post
     * [{"name":"mct","email":"mct@qq.com","age":"21"},
     * {"name":"mct1","email":"mct1@qq.com","age":"21"}]
     * @param students
     * @return
     */
    @PostMapping(value = "/students/v2", consumes = "application/json")
    public Result<Object> insertStudents(@RequestBody List<Student> students) {
        return studentService.insertStudents(students);
    }
}
